package com.quanyan.place.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.place.entity.apireq.*;
import com.quanyan.place.service.PlacePromoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * Created by yuanxuebing on 16/12/13.
 */
@RestController
@RequestMapping("/admin/api/promotion")
public class PlacePromoteController {
    @Autowired
    private PlacePromoteService placePromoteService;

    /**
     * 获取推广订单列表
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getPlacePromoteOrders", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getPlacePromoteOrders(@RequestBody @Valid ReqPromoteOrders reqPromoteOrders, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        APIResponse apiResponse = placePromoteService.getPlacePromoteOrders(reqPromoteOrders);
        return apiResponse;
    }

    /**
     * 获取推广订单列表扩展
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getPlacePromoteOrdersEx", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getPlacePromoteOrdersEx(@RequestBody @Valid ReqPromoteOrders reqPromoteOrders, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        APIResponse apiResponse = placePromoteService.getPlacePromoteOrdersEx(reqPromoteOrders);
        return apiResponse;
    }


    /**
     * 场馆管理后台 获取推广订单列表
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getPlacePromotionByAdmin", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getPlacePromotionByAdmin(@RequestBody @Valid ReqPromotionByAdmin reqPromotionByAdmin, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        APIResponse apiResponse = placePromoteService.getPlacePromotionByAdmin(reqPromotionByAdmin);
        return apiResponse;
    }

    /**
     * 推广结算
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/settlePromotions", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> settlePromotions(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        placePromoteService.promoteSettleTask();
        return APIResponse.returnSuccess();
    }

    /**
     * 获取推广佣金明细
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getPromoteRewards", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getPromoteRewards(@RequestBody @Valid ReqPromoteRewards reqPromoteRewards, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        APIResponse apiResponse = placePromoteService.getPromoteRewards(reqPromoteRewards);
        return apiResponse;
    }

    /**
     * 获取推广提现记录
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getPromoteWithdrawals", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getPromoteWithdrawals(@RequestBody @Valid ReqPromoteWithdrawals reqPromoteWithdrawals, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        APIResponse apiResponse = placePromoteService.getPromoteWithdrawals(reqPromoteWithdrawals);
        return apiResponse;
    }

    /**
     * 获取推广佣金对账
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getPromoteReconciliation", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getPromoteReconciliation(@RequestBody @Valid ReqPromoteReconciliation reqPromoteReconciliation, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        APIResponse apiResponse = placePromoteService.getPromoteReconciliation(reqPromoteReconciliation);
        return apiResponse;
    }

    /**
     * 推广佣金结算
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/settlePromotionRewards", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> settlePromotionRewards(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        try {
            placePromoteService.promoteRewardSettleTask();
        } catch (Exception e) {
            e.printStackTrace();
            return APIResponse.returnFail(e.toString());
        }
        return APIResponse.returnSuccess();
    }
}
